﻿#include "productplanstandarditemmodel.h"
#include "company.h"

ProductPlanStandardItemModel::ProductPlanStandardItemModel(QObject *parent) : QStandardItemModel(parent)
{
    header<<"商品ID"
         <<"型号	"
        <<"型号拼音码"
       <<"名称"
      <<"名称拼音码"
     <<"单位"
    <<"下限"
    <<"上限"
    <<"库存数量"
    <<"净价"
    <<"计划数量"
    <<"计划金额"
    <<"货号"
    <<"品牌"
    <<"产地"
    <<"规格"
    <<"条型码"
    <<"备注"
    <<"指导价格1"
    <<"指导价格2"
    <<"一级类别"
    <<"二级类别";
    setHorizontalHeaderLabels(header);
}

bool ProductPlanStandardItemModel::setData(const QModelIndex &index, const QVariant &value, int role){
    if(index.column() != 10){
        return false;
    }

    double taxRate = Company::getTaxRate().toDouble();
    if(taxRate <= 0){
        taxRate = 1;
    }

    Product *product = products[index.row()];
    int count = value.toInt();
    if(count < 0) count=0;
    product->planCount = count;
    QStandardItemModel::setData(index, count, role);
    QStandardItemModel::setItem(index.row(), 11, new QStandardItem(QString::number(product->inPrice*count*taxRate, 'f', 2)));

    return true;
}

QVariant ProductPlanStandardItemModel::data(const QModelIndex &index, int role) const{
    QVariant value = QStandardItemModel::data(index, role);
    //设置内容居中
    if(role==Qt::TextAlignmentRole){
        return  Qt::AlignCenter;
    }

    //设置内容颜色
    if (role == Qt::TextColorRole && (index.column() == 10))
        return QVariant::fromValue(QColor(Qt::blue));
    return value;
}

void ProductPlanStandardItemModel::reset(){
    clear();
    products.clear();
    setHorizontalHeaderLabels(header);
}

void ProductPlanStandardItemModel::insertProduct(Product &product, double taxRate){
    QList<QStandardItem*> rowData;

    product.planCount = product.upperLimit - product.count;

    rowData.append(new QStandardItem(product.productId));
    rowData.append(new QStandardItem(product.type));
    rowData.append(new QStandardItem(product.typePinyin));
    rowData.append(new QStandardItem(product.name));
    rowData.append(new QStandardItem(product.namePinyin));
    rowData.append(new QStandardItem(product.unit));
    rowData.append(new QStandardItem(QString("%1").arg(product.lowerLimit)));
    rowData.append(new QStandardItem(QString("%1").arg(product.upperLimit)));
    rowData.append(new QStandardItem(QString("%1").arg(product.count)));
    rowData.append(new QStandardItem(QString::number(product.inPrice, 'f', 2)));
    rowData.append(new QStandardItem(QString("%1").arg(product.upperLimit - product.count)));
    rowData.append(new QStandardItem(QString::number(product.planCount*product.inPrice*taxRate, 'f', 2)));
    rowData.append(new QStandardItem(product.productNum));
    rowData.append(new QStandardItem(product.brand));
    rowData.append(new QStandardItem(product.originPlace));
    rowData.append(new QStandardItem(product.specifications));
    rowData.append(new QStandardItem(product.barCode));
    rowData.append(new QStandardItem(product.remark));
    rowData.append(new QStandardItem(QString::number(product.guidePrice1, 'f', 2)));
    rowData.append(new QStandardItem(QString::number(product.guidePrice2, 'f', 2)));
    rowData.append(new QStandardItem(product.primaryDirectory));
    rowData.append(new QStandardItem(product.secondaryDirectory));

    products.append(&product);
    insertRow(rowCount()-1, rowData);
}

void ProductPlanStandardItemModel::insertProduct(QList<Product*> products){
    double taxRate = Company::getTaxRate().toDouble();
    if(taxRate <= 0){
        taxRate = 1;
    }

    taxRate = 1+(taxRate/100);

    int i=0;

    for(Product *product:products){
        if(product->checkExists(this->products)){
            i++;
        }else{
            insertProduct(*product, taxRate);
        }
    }

    if(i!=0){
        Tools::showRepeatProductMsg();
    }
}
